package com.xjm.i1.core.resolver;

import com.xjm.i1.constant.AreaEnum;
import com.xjm.i1.constant.ColorEnum;
import com.xjm.i1.constant.MinDensityEnum;
import com.xjm.i1.convert.ColorDiffDensityConvert;
import com.xjm.i1.core.struct.location.MinDataPointMap;
import com.xjm.i1.core.struct.location.Point;
import com.xjm.i1.entity.ColorDiffDensityArea;
import com.xjm.i1.core.struct.node.load.I1AreaCollections;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.List;

/**
 * i1原始数据中色差与密度解析器
 **/
@Data
public abstract class AbstractColorDiffDensityResolver implements MetaDataResolver {

    // 数据页的区域
    private AreaEnum areaSign;

    // 数据加载坐标映射
    private List<MinDataPointMap> pointMaps;

    public AbstractColorDiffDensityResolver(AreaEnum areaSign, List<MinDataPointMap> pointMaps) {
        this.areaSign = areaSign;
        this.pointMaps = pointMaps;
    }

    @Override
    public boolean resolverMatch(AreaEnum areaEnum) {
        return this.areaSign == areaEnum;
    }

    @Override
    public I1AreaCollections resolve(Double[][] metaData) throws NoSuchFieldException, IllegalAccessException {
        int initDiffDensityArea = ColorEnum.getMinColorEnumOrdered().length * MinDensityEnum.values().length;
        I1AreaCollections res = new I1AreaCollections(initDiffDensityArea);
        for (MinDataPointMap pointMap : pointMaps) {
            ColorDiffDensityArea bean = ColorDiffDensityConvert.INSTANCE.convert(pointMap);
            bean.setAreaSign(areaSign);
            for (Point point : pointMap.getPoints()) {
                double val = metaData[point.getX()][point.getY()];
                String mapV = point.getMapValue();

                Field declaredField = bean.getClass().getDeclaredField(mapV);
                declaredField.setAccessible(true);
                declaredField.set(bean, val);
            }
            res.getColorDiffDensityArea().add(bean);
        }
        return res;
    }
}